<?php

namespace Caisse\CaisseBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Caisse\CaisseBundle\Entity\Commande;
use Caisse\CaisseBundle\Form\CommandeType;
use Symfony\Component\HttpFoundation\Response;

class CommandeController extends Controller
{
  
    public function listCommandeAction()
    {
        $em = $this->getDoctrine()->getManager();
        $commandes = $em->getRepository('CaisseBundle:Commande')->findAll();
        
        return $this->render('CaisseBundle:Commande:listCommande.html.twig', array('commandes'=>$commandes,
            ));
    }


    public function AjoutCommandeAction()
    {
     $em = $this->getDoctrine()->getManager();
     $request = $this->getRequest();
     $id = $request->get('id');
     if ($id != ''){
         $commande = $em->getRepository('CaisseBundle:Commande')->find($id);
     }else
         $commande = new Commande();
         $commande->setDate(new \DateTime());
          $form = $this->createForm(new CommandeType(), $commande);
          
          if ($request->isMethod('Post'))
          {
              $commande->bind($request);
              if ($form->isValid()){
                  
                 $commande = $form->getData();
                 $repository = $em->getRepository('CaisseBundle:Commande');
                $em->persist($commande);
                 $em->flush();
                 $commandes = $repository->findAll();
              return $this->render('CaisseBundle:Commande:listCommande.html.twig',array('commandes'=>$commandes));
          }  
          
         } 
     return $this->render('CaisseBundle:Commande:AjoutCommande.html.twig', array('form'=> $form->createView(),'id'=> $id));

    }
    
   
    
}
